package com.example.dllo.my36kr.search;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.news.NewsAdapter;
import com.example.dllo.my36kr.news.NewsBean;
import com.example.dllo.my36kr.news.newsecond.SecondActivity;
import com.example.dllo.my36kr.news.newsecond.SecondBean;
import com.example.dllo.my36kr.news.newsecond.third.ThirdActivity;
import com.example.dllo.my36kr.tools.BaseAty;
import com.example.dllo.my36kr.tools.MyApp;
import com.example.dllo.my36kr.tools.NetTool;
import com.example.dllo.my36kr.tools.StaticUrl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by dllo on 16/8/24.
 */
public class SearchActivity extends BaseAty {

    private ImageButton searchIb;
    private Button cancelBt;
    private EditText searchEt;
    private ListView listView;
    private NewsAdapter adapter;
    private String search;
    private LinearLayout hideLl;
    boolean head = false;
    private View view;


    @Override
    protected int setLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        //抬头布局
        view = LayoutInflater.from(this).inflate(R.layout.activity_search_more, null);
        listView = bindView(R.id.lv_activity_search);
        adapter = new NewsAdapter(MyApp.getContext());
        searchIb = bindView(R.id.ib_activity_search);
        cancelBt = bindView(R.id.bnt_activity_search);
        searchEt = bindView(R.id.et_activity_search);
        hideLl = bindView(R.id.ll_activity_search);


        searchEt.addTextChangedListener(new TextWatcher() {
            private String ss;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                search = searchEt.getText().toString();
                try {
                    ss = URLEncoder.encode(search,"utf-8");//转码
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (s.length() > 0){
                    mNetTool.getNetData(StaticUrl.SEARCH_URL + ss + "&page=1&pageSize=3", NewsBean.class, new NetTool.NetListener<NewsBean>() {
                        @Override
                        public void onSuccess(NewsBean newsBean) {
                            adapter.setNewsBean(newsBean);
                            listView.setAdapter(adapter);
                            hideLl.setVisibility(View.GONE);
                            skip(newsBean,ss);
                            if (head == false){//去 抬头
                                listView.addHeaderView(view);
                                head = true;
                            }
                        }
                        @Override
                        public void onError(String errorMsg) {
                        }
                    });
                }

            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        cancelBt.setOnClickListener(new View.OnClickListener() {//取消按键
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void skip(final NewsBean newsBean, final String s){//搜索时 显示的listView 跳转
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0){//向第三也传值
                    Intent intent = new Intent(SearchActivity.this, ThirdActivity.class);
                    intent.putExtra("more", s);
                    intent.putExtra("search",search);
                    startActivity(intent);
                }else {
                    Intent intent = new Intent(SearchActivity.this, SecondActivity.class);//传回详情页
                    intent.putExtra("details", newsBean.getData().getData().get(position - 1).getFeedId());
                    startActivity(intent);
                }
            }

        });
    }
    @Override
    protected void initData() {
        searchIb.setOnClickListener(new View.OnClickListener() {
            private String a;

            @Override
            public void onClick(View v) {
                search = searchEt.getText().toString();
                try {
                    a = URLEncoder.encode(search,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                mNetTool.getNetData(StaticUrl.SEARCH_URL + a + "&page=1&pageSize=3", NewsBean.class, new NetTool.NetListener<NewsBean>() {//网址 拼接
                    @Override
                    public void onSuccess(final NewsBean newsBean) {
                        adapter.setNewsBean(newsBean);
                        listView.setAdapter(adapter);
                        if (head == false) {//去 抬头
                            listView.addHeaderView(view);
                            head = true;
                        }
                        hideLl.setVisibility(View.GONE);
                        skip(newsBean,a);//内部方法
                    }
                    @Override
                    public void onError(String errorMsg) {
                    }
                });
            }
        });
    }
}
